<script setup>
import {ElMessage, ElMessageBox} from "element-plus";
import {getUser, logout, userIcon} from "@/components/userInfo.js";
import VueCookie from "vue-cookie";
import {useRouter, useRoute} from 'vue-router'
import {computed} from 'vue'



const router = useRouter()
const route = useRoute()


// 判断当前是否在用户中心或其子路由
const isUserCenterRoute = computed(() => {
    return route.path.startsWith('/user/center')
})

const goToUserCenter = () => {
    if (isUserCenterRoute.value) {
        router.go(0)
    } else {
        router.push('/user/center')
    }
}
const token = VueCookie.get('sessionId')

const confirmLogout = async () => {
    try {
        await ElMessageBox.confirm(
            '确定要退出登录吗?',
            '提示',
            {
                confirmButtonText: '确认',
                cancelButtonText: '取消',
                type: 'warning',
            }
        )
        logout()
        window.location = '/'
    } catch (e) {
        ElMessage({
            type: 'info',
            message: '取消操作',
        })
    }
}
const goToOther = () => {
    if (getUser.value.role === 2) {
        router.push('/sup')
    }
    if (getUser.value.role === 1) {
        router.push('/mer')
    }
}

</script>

<template>
    <div>
        <div class="user_center">
            <div v-if="token" class="icon">
                <img :src="userIcon" alt="">
            </div>
            <div v-else class="noLogin">
                <div>登录</div>
            </div>


            <div class="child">

                <div v-if="token" class="login-true">

                    <el-text class="userName" truncated>
                        {{ getUser.username }}
                    </el-text>

                    <div class="user-role">
                        <div v-if="getUser.role === 2">
                            <span>【管理员】</span>
                            <el-button text @click="goToOther">
                                管理员主页
                            </el-button>
                        </div>
                        <div v-if="getUser.role === 1">
                            <span>【商家】</span>
                            <el-button text @click="goToOther">
                                商家主页
                            </el-button>
                        </div>
                        <div v-if="getUser.role === 0">
                            <span>【普通用户】</span>
                        </div>
                    </div>
                    <el-button
                            :class="{'ifCenter': isUserCenterRoute}"
                            @click="goToUserCenter"
                            text>
                        个人中心
                    </el-button>

                    <el-button text @click="confirmLogout">
                        退出登录
                    </el-button>
                </div>

                <div class="toLogin" v-else>
                    <div class="show-login">
                        登陆后享受更多服务
                    </div>
                    <router-link to="/login">
                        <el-button
                                type="primary"
                                class="login-btn">
                            立即登录
                        </el-button>
                    </router-link>
                    <div class="show-register">首次使用？
                        <router-link to="/register">
                            点我注册
                        </router-link>
                    </div>
                </div>
            </div>
        </div>
    </div>
</template>

<style scoped lang="scss">
.userName {
  font-weight: 700;
  margin: 10px;
  text-align: center;
  overflow: hidden;
  font-size: 1rem;
  width: 100px;
  color: #ff7200;
}

.ifCenter {
  color: #4250ff;
  font-weight: 700;
}

.user-role {
  > div > span {
    color: #00bb22;
    display: flex;
    font-weight: 700;
    text-align: center;
    justify-content: center;
    margin-bottom: 5px;
  }

  > div > button {
    margin-top: 10px;
  }
}

.show-login {
  display: flex;
  align-items: center;
  justify-content: center;
  color: #fff;
  font-weight: 700;
  font-size: 1.2rem;
  border-radius: 5px;
  background-image: url('@/components/icons/show-login.jpg');
  background-size: cover;
  background-position: center;
  width: 200px;
  height: 100px;
}

.login-btn {
  width: 200px;
}

.noLogin {
  background-color: #61a7ff;
  display: flex;
  text-align: center;
  font-weight: 700;
  width: 55px;
  height: 55px;
  font-size: 1.2rem;
  color: #fff;
  border-radius: 50%;
  justify-content: center;
  align-items: center;
  flex-direction: column;
}

.toLogin {
  display: flex;
  justify-content: center;
  align-items: center;
  flex-direction: column;

  .show-register {
    font-size: 0.8rem;

    > a {
      color: #409eff;
    }
  }
}

.el-button {
  margin: 5px;
}

.login-true {
  display: flex;
  justify-content: center;
  align-items: center;
  flex-direction: column;
}

.user_center {
  display: flex;
  justify-content: center;
  align-items: center;
  flex-direction: column;
  position: relative;

  .icon {
    display: flex;
    justify-content: center;
    align-items: center;
    width: 70px;
    height: 70px;
    z-index: 10;

    img {
      width: 50px;
      height: 50px;
      border-radius: 50%;
      transition: all 0.3s;
    }
  }

  .child {
    width: auto;
    height: auto;
    background-color: #fff;
    position: absolute;
    display: flex;
    padding: 20px;
    align-items: center;
    flex-direction: column;
    top: 60px;
    border-radius: 10px;
    border: 1px solid #ccc;
    box-shadow: 0 0 10px rgba(0, 0, 0, 0.1);
    transition: all 0.3s;
    z-index: 5;

    opacity: 0;
    visibility: hidden;
  }

  &:hover {
    img {
      width: 70px;
      height: 70px;
      transform: translateY(10px);
    }

    .child {
      opacity: 1;
      visibility: visible;
    }
  }
}


</style>